const app = getApp();
const util = require('../../utils/util.js')
Page({
    data: {
        imgUrl: app.globalData.imgUrl,
        areaList:[],
    },
    onLoad: function () {
        wx.setNavigationBarTitle({
            title: '区域管理'
        })
        this.getAreaList()
    },
    onShow: function () {
        this.getAreaList()
    },
    onPullDownRefresh:function (){
        this.getAreaList()
    },
    getAreaList (){
        wx.showLoading({
            title: '加载中',
        });
        var that = this;
        wx.request({
            url: app.globalData.APIURL + '/vending-admin/operator/getAllEquipAreas',
            data: {
                token: app.globalData.token,
            },
            // method:'GET',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                // 停止刷新
                wx.hideLoading();
                console.log(res.data)
                if (app.requestEro(res.data.success, res.data.msg)) {
                    that.setData({
                        areaList:res.data.equipAreaList
                    })
                }

            },
            fail: function (err) {
                console.log(err);
                wx.hideLoading();
            }
        })
    },
    addArea (){
        wx.navigateTo({
            url:'/pages/area-add/area-add'
        })
    },
    gotoAreaDetail (e){
        console.log(e.currentTarget.dataset.id);
        let id  = e.currentTarget.dataset.id;
        let currentArea = this.data.areaList.filter(function (item) {
            return item.id === id;
        });
        let detail = JSON.stringify(currentArea[0]);
        wx.navigateTo({
            url:'/pages/area-detail/area-detail?detail='+detail+'&edit=0',
        })
    },
    editArea (e){
        console.log(e);
        let id  = e.currentTarget.dataset.id;
        let currentArea = this.data.areaList.filter(function (item) {
            return item.id === id;
        });
        let detail = JSON.stringify(currentArea[0]);
        wx.navigateTo({
            url:'/pages/area-detail/area-detail?detail='+detail+'&edit=1',
        })
    }
})